package com.wisdytech.linkdcs.equipment.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.session.UserSession;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipment.model.CollectionParam;
import com.wisdytech.linkdcs.equipment.model.EquipModel;
import com.wisdytech.linkdcs.equipment.service.IEquipmentModelService;
import com.wisdytech.linkdcs.equipment.service.IEquipmentParamViewService;
import com.wisdytech.linkdcs.equipment.vo.EquipmentParamViewVO;
import com.wisdytech.linkdcs.system.model.SysUser;

/**
 * 
 * <p>
 * ClassName: EquipmentParamAction
 * </p>
 * <p>
 * Description: 
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-26
 * </p>
 */
@Controller
@RequestMapping(value = "/equipment/paramView")
public class EquipmentViewParamAction {
    
    @Autowired
    @Qualifier("equipmentParamViewService")
    private IEquipmentParamViewService equipmentParamViewService;
    @Autowired
    @Qualifier("equipmentModelService")
    private IEquipmentModelService equipmentModelService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    
    @RequestMapping("/index")
    public String index(Model model, String modelCode, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        if (StringUtils.isNotBlank(modelCode)){
            model.addAttribute("modelCode", modelCode);
        }
        return "equipment/equipRegistered/modelMange/visualization";
    }
    
    /**
     * 分页条件查询参数可视化管理页面信息
     * 
     * @param emModel 设备类别编号
     * @param modelType 设备类别型号
     * @param filter 分页参数
     * @return 分页查询结果集
     * @throws IOException 
     */
    @RequestMapping(value = "/listEuipmentParamView/get.mvc")
    @ResponseBody
    public StandardTableResult<EquipmentParamViewVO> listEuipmentParamView(String modelCode, String showLevel, String modelType, Filter filter) throws IOException {
//    	String newEmModelType = new String(emModelType.getBytes("iso8859-1"),"UTF-8");
    	PageInfo<CollectionParam> equipParamViews= equipmentParamViewService.listParamViewByFilter(modelCode, showLevel, modelType, filter);
       //转换为前台结果集
       List<CollectionParam> paramViewList = equipParamViews.getList();
       List<EquipmentParamViewVO> paramViewVOList=new ArrayList<EquipmentParamViewVO>();
       for(CollectionParam param:paramViewList){
           paramViewVOList.add(DataTransformUtil.changeParamViewVO(param));
       }
       return new StandardTableResult<EquipmentParamViewVO>(equipParamViews.getTotal(),paramViewVOList);
    }
    /**
     * <p>Description: 保存参数可视化管理对象</p> 
     * @param param 参数对象
     * @param emModel 设备类别编号
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveCollectionParamView/save.mvc")
    @ResponseBody
    public StandardResult saveCollectionParamView(CollectionParam param, HttpSession session) {
        SysUser userSession = BaseUtil.getCurrentUser();
        String loginName = userSession.getUsername();
        if (StringUtils.isBlank(param.getPkId())) {
            return equipmentParamViewService.addCollectionParamView(param, loginName);
        }else {
            return equipmentParamViewService.updateCollectionParamView(param, loginName);
        }
    }
    /**
     * 删除设备参数可视化信息
     * @param pkId 参数管理标识id
     * @return 结果对象
     */
    @RequestMapping(value = "/delCollectionParamView/del.mvc")
    @ResponseBody
    public StandardResult delCollectionParamView(String pkId) {
        return equipmentParamViewService.delCollectionParamView(pkId);
    }
    /**
     * <p>Description: 点击修改时带出对应参数可视化信息</p> 
     * @param emModel 设备类别编号
     * @param pkId 参数唯一id
     * @return 参数可视化管理前台显示对象
     */
    @RequestMapping(value = "/getEuipmentParamViewById/get.mvc")
    @ResponseBody
    public EquipmentParamViewVO getEuipmentParamViewById(String emModel, String pkId) {
        CollectionParam param = equipmentParamViewService.getEuipmentParamViewById(pkId);
        EquipModel model = equipmentModelService.getEuipmentModelByEmModel(emModel);
        EquipmentParamViewVO paramViewVO = DataTransformUtil.changeParamViewVO(param, model);
        return paramViewVO;
    }
    
}
